import 'package:flutter/material.dart';
import 'package:flutter_auto_login/Screens/Login/components/background.dart';
import 'package:flutter_auto_login/Screens/Signup/signup_screen.dart';
import 'package:flutter_auto_login/components/already_have_an_account_check.dart';
import 'package:flutter_auto_login/components/custom_textfield_widget.dart';
import 'package:flutter_auto_login/components/rounded_button.dart';
// import 'package:flutter_auto_login/components/rounded_input_field.dart';
// import 'package:flutter_auto_login/components/rounded_password_field.dart';
import 'package:flutter_auto_login/utils/toast_utils.dart';
import 'package:flutter_svg/flutter_svg.dart';

class Body extends StatefulWidget {
  const Body({Key key}) : super(key: key);

  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  // 密码输入框焦点控制
  FocusNode _passwordFocusNode = new FocusNode();
  // 手机号/用户名输入框焦点控制
  FocusNode _userNameFocusNode = new FocusNode();
  // 手机号输入框控制器
  TextEditingController _userNameEditController = new TextEditingController();
  // 密码输入框控制器
  TextEditingController _userPasswordEditController =
      new TextEditingController();
  // 是否显示密码
  bool _passwordShow = true;
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Background(
      child: SingleChildScrollView(
        child: Column(
          children: [
            Text(
              "LOGIN",
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),
            ),
            SizedBox(height: size.height * 0.03),
            SvgPicture.asset(
              "assets/icons/login.svg",
              height: size.height * 0.35,
            ),
            SizedBox(height: size.height * 0.03),
            //  RoundedInputField(
            //   hintText: "Your Email",
            //   onChanged: (value) {},
            // ),
            // RoundedPasswordField(
            //   onChanged: (value) {},
            // ),
            TextFieldWidget(
              hintText: "手机号",
              submit: (value) {
                if (value.length != 11) {
                  ToastUtils.showToast("请输入11位手机号");
                  //手机号输入框重新获取焦点
                  FocusScope.of(context).requestFocus(_userNameFocusNode);
                  return;
                }
                //手机号输入框失去焦点
                _userNameFocusNode.unfocus();
                //密码输入框获取焦点
                FocusScope.of(context).requestFocus(_passwordFocusNode);
              },
              focusNode: _userNameFocusNode,
              prefixIconData: Icons.phone,
              controller: _userNameEditController,
              obscureText: false,
            ),
            SizedBox(height: size.height * 0.03),
            TextFieldWidget(
              // 占位符
              hintText: "密码",
              // 提交事件
              submit: (value) {
                if (value.length < 6) {
                  ToastUtils.showToast("请输入6位以上密码");
                  //密码输入框获取焦点
                  FocusScope.of(context).requestFocus(_passwordFocusNode);
                  return;
                }
                //手机号输入框失去焦点
                _userNameFocusNode.unfocus();
                //密码输入框失去焦点
                _passwordFocusNode.unfocus();
              },
              // 焦点控制器
              focusNode: _passwordFocusNode,
              // 左侧图标占位
              prefixIconData: Icons.lock_open_outlined,
              // 右侧图标占位
              suffixIconData:
                  _passwordShow ? Icons.visibility : Icons.visibility_off,
              // 是否显示右侧图标
              obscureText: _passwordShow,
              // 输入框控制器
              controller: _userPasswordEditController,
              // 右侧图标点击事件
              onTap: () {
                //设置密码输入框输入内容可见与不可见
                setState(() {
                  _passwordShow = !_passwordShow;
                });
              },
            ),
            RoundedButton(
                text: "LOGIN",
                press: () {
                  submitFunction();
                }),
            SizedBox(height: size.height * 0.03),
            AlreadyHaveAnAccountCheck(
                login: true,
                press: () {
                  Navigator.pushAndRemoveUntil(
                    context,
                    MaterialPageRoute(
                        builder: (BuildContext context) => SignUpScreen()),
                    (route) => false,
                  );
                }),
          ],
        ),
      ),
    );
  }

  ///登录功能
  void submitFunction() async {
    //获取用户名
    String userName = _userNameEditController.text;
    //获取密码
    String password = _userPasswordEditController.text;

    if (userName.trim().length != 11) {
      ToastUtils.showToast("请输入11位手机号");
      return;
    }
    if (password.trim().length < 6) {
      ToastUtils.showToast("请输入6位以上密码");
      return;
    }

    Map<String, dynamic> map = {
      "mobile": userName,
      "password": password,
    };

    print(map);
  }
}
